package com.linjian.togetherbirthday.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.view.View;

import com.linjian.togetherbirthday.MainActivity;
import com.linjian.togetherbirthday.R;
import com.linjian.togetherbirthday.base.BaseFragment;
import com.linjian.togetherbirthday.bean.BottomNavigationBean;
import com.linjian.togetherbirthday.view.BottomBarTab;
import com.linjian.togetherbirthday.view.BottomNavigationBar;

import java.util.ArrayList;

public class HomeFragment extends BaseFragment implements View.OnClickListener {

    private BottomNavigationBar bottomLayout;
    private FloatingActionButton floatingActionButton;

    MainActivity activity;

    private ArrayList<BottomNavigationBean> list = new ArrayList<>();

    public static HomeFragment newInstance() {
        HomeFragment homeFragment = new HomeFragment();
        Bundle bundle = new Bundle();
//        bundle.putInt(Integer.class.getName(), resId);
        homeFragment.setArguments(bundle);
        return homeFragment;
    }


    @Override
    protected int setView() {
        return R.layout.fragment_main;
    }

    @Override
    protected void init(View view) {
        activity = (MainActivity)getActivity();

        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.container, HomeOneFragment.newInstance())
                .commitAllowingStateLoss();
        setUpBottomNavigationBar(view);
        floatingActionButton = view.findViewById(R.id.fab);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        floatingActionButton.setOnClickListener(this);
    }

    public void setUpBottomNavigationBar(View view) {

        bottomLayout = (BottomNavigationBar) view.findViewById(R.id.bottomLayout);
        bottomLayout.setTabWidthSelectedScale(1.5f);
        bottomLayout.setTextDefaultVisible(true);
        bottomLayout.setTextColorResId(R.color.colorGrayOne);

        bottomLayout.addTab(R.mipmap.home_page_normal, "首页", 0xffffffff);
        bottomLayout.addTab(R.mipmap.found_normal, "发现", 0xffffffff);
        bottomLayout.addTab(R.mipmap.messge_normal, "消息", 0xffffffff);
        bottomLayout.addTab(R.mipmap.me_normal, "我的", 0xffffffff);

        bottomLayout.setOnTabListener(new BottomNavigationBar.TabListener() {
            @Override
            public void onSelected(BottomBarTab tab, int position) {
                Fragment fragment = null;
                switch (position) {
                    case 0:
                        fragment = HomeOneFragment.newInstance();
                        break;
                    case 1:
                        fragment = HomeTwoFragment.newInstance();
                        break;
                    case 2:
                        fragment = HomeThreeFragment.newInstance();
                        break;
                    case 3:
                        fragment = HomeFourFragment.newInstance();
                        break;
                    default:
                        fragment = HomeOneFragment.newInstance();
                        break;
                }
                getActivity().getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.container, fragment)
//                        .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
                        .commitAllowingStateLoss();
            }
        });
        Handler handler =new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                bottomLayout.requestLayout();
            }
        },1000);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fab:
                activity.replaceFragment("ReleaseDateFragment","");
                break;
        }
    }

}

